package com.xsm.ppsecret.presenter;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.google.gson.Gson;
import com.xsm.ppsecret.model.PwdEntry;
import com.xsm.ppsecret.sql.DBHelper;
import com.xsm.ppsecret.sql.PPUserSql;
import com.xsm.ppsecret.ui.implview.IAddPwdActivity;
import com.xsm.ppsecret.util.JsonUtils;

/**
 * Author: Xiasem
 * Email: xiasem@163.com
 * Date: 17-4-18.
 */

public class AddPwdPresenter extends BasePresenter {

    private IAddPwdActivity mAddPwdActivity;

    public AddPwdPresenter(IAddPwdActivity addPwdActivity, Context context) {
        mAddPwdActivity = addPwdActivity;
    }

    public void save(Context context, String title, String username, String password, String remarks) {
        if (title.isEmpty()) {
            mAddPwdActivity.saveError("标题不能为空");
            return;
        }
        PwdEntry entry = new PwdEntry();

        entry.setTitle(title);
        entry.setUsername(username);
        entry.setPassword(password);
        entry.setRemarks(remarks);
        String json = JsonUtils.getInstance().serialize(entry);
        DBHelper dbHelper = new DBHelper(context);
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        boolean b = PPUserSql.savePwdEntry(database, json);
        if (b) {
            mAddPwdActivity.saveOk();
        } else {
            mAddPwdActivity.saveError("保存失败，请重新存储");
        }
    }

}
